package wangwenjun.phase2.concurrencypattern.threadcontext.context2;

/**
 * @author ChangLiang
 * @date 2020/6/15
 */
public class ThreadContextV2 {

    /**
     * 可以选择为每一个数据定义一个ThreadLocal
     *
     * 第二种方式使用了两个ThreadLocal,
     * 那么与之对应的ThreadLocalMap 中将会存在两个Entry
     */
    private static final ThreadLocal<Configuration> configuration = ThreadLocal.withInitial(Configuration::new);

    private static final ThreadLocal<OtherResource> otherResource = ThreadLocal.withInitial(OtherResource::new);

    public static void setConfiguration(Configuration conf) {
        configuration.set(conf);
    }

    public static void setOtherResource(OtherResource resource) {
        otherResource.set(resource);
    }

    public static Configuration getConfiguration() {
        return configuration.get();
    }

    public static OtherResource getOtherResource() {
        return otherResource.get();
    }
}
